package com.vids.runner;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.vids.model.entity.User;
import com.vids.model.entity.VIIDServer;
import com.vids.service.IUserService;
import com.vids.service.IVIIDServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.vids.constant.GlobalConstant.VIID_ID;
import static com.vids.constant.GlobalConstant.VIID_ID_KEY;

/**
 * @author ly
 */
@Component
public class VIIDRunner implements CommandLineRunner {

    public static Map<String, VIIDServer> selfViidServer = new ConcurrentHashMap<>(1);
    public static Map<String, User> remoteViidUser = new ConcurrentHashMap<>();
    public String SELF_VIID_SERVER_ID;
    public static Map<String, String> deviceIdRelateMap = new ConcurrentHashMap<>();

    @Autowired
    private IVIIDServer viidService;
    @Autowired
    private IUserService userService;



    @Override
    public void run(String... args) {
        getSelfViidServer();
        setRemoteViidUser();
    }

    public void getSelfViidServer(){
        VIIDServer VIIDServer = viidService.getById(VIID_ID);
        selfViidServer.put(VIID_ID_KEY, VIIDServer);
        SELF_VIID_SERVER_ID = VIIDServer.getVIIDServerID();
    }

    /**
     * 第三方联网平台的用户密码信息 remoteViidUser
     * 10分钟更新一次缓存
     */
    @Scheduled(fixedRate = 10*60*1000, initialDelay = 13000)
    public void setRemoteViidUser(){
        List<VIIDServer> viidServerList = viidService.list(null);
        if(CollUtil.isEmpty(viidServerList)){
            return;
        }
        for (VIIDServer VIIDServer : viidServerList) {
            if(VIIDServer.getId().equals(VIID_ID)){
                continue;
            }
            String viidServerID = VIIDServer.getVIIDServerID();
            User user = userService.findByPlatformAppId(viidServerID);
            if(ObjectUtil.isNotNull(user)){
                remoteViidUser.put(viidServerID, user);
            }
        }
    }



    /**
     * 本级联网平台信息
     * @return
     */
    public String getSelfViidServerId(){
        if(CollUtil.isEmpty(selfViidServer)){
            getSelfViidServer();
            if(CollUtil.isEmpty(selfViidServer)){
                return "1";
            }
        }
        return selfViidServer.get(VIID_ID_KEY).getVIIDServerID();
    }

    public String getRemoteViidUsername(String remoteViid){
        if(CollUtil.isEmpty(remoteViidUser)){
            getSelfViidServer();
            if(CollUtil.isEmpty(remoteViidUser)){
                return "null";
            }
        }
        User User = remoteViidUser.get(remoteViid);
        if(ObjectUtil.isNull(User)){
            return "null";
        }
        if(StrUtil.isBlank(User.getLogonName())){
            return "null";
        }
        return User.getLogonName();
    }

    public String getRemoteViidPassword(String remoteViid){
        if(CollUtil.isEmpty(remoteViidUser)){
            getSelfViidServer();
            if(CollUtil.isEmpty(remoteViidUser)){
                return "null";
            }
        }
        User User = remoteViidUser.get(remoteViid);
        if(ObjectUtil.isNull(User)){
            return "null";
        }
        if(StrUtil.isBlank(User.getPassword())){
            return "null";
        }
        return User.getPassword();
    }

}
